<?php
/**
 * Created by PhpStorm.
 * User: ttl
 * Date: 2019/5/18
 * Time: 16:58
 */

namespace app\admin\controller;

use think\Console;
use think\Controller;
use think\Image;
use think\Request;
use think\Validate;
use think\Db;

class Base extends Controller
{
    protected $req; //用来处理客户端传递过来的参数
    protected $validater; //用来验证数据/参数
    protected $params; //过滤后符合要求的参数
    protected $user;

    protected $rules = array();
    protected $nocheckArr = array();
    protected $body_data = [];
    /**
     * [构造方法]
     * @return [type] [description]
     */
    protected function _initialize()
    {

        parent::_initialize();
        $this->req = Request::instance();
        $funstr =  $this->req->action();
        if(!in_array($funstr, $this->nocheckArr)) {
            //1. 验证token

//            $data = I('get.');
//            var_dump(json_encode(I('.')));
//            var_dump(I('att/a', []));
//            $this->checkToken($data);
//            //获取body里的参数
//            $bodyData = @file_get_contents('php://input');
////将获取到的值转化为数组格式
//            $bodyData = json_decode($bodyData,true);
//            var_dump($this->req->param());
//            var_dump(input('get.'));
//            var_dump(I('get.'));
//            var_dump($this->req->getInput());
//            exit();
//            $this->checkToken($this->req->param());
            $r_data = $this->req->param();
            if($this->req->getInput()) {
                $r_data = json_decode($this->req->getInput(), true);
            }

            $this->checkToken($r_data);
        }
    }

    //验证token方法 (防止篡改数据)
    /*
    $arr: 全部请求参数
    return : json
    */
    protected function checkToken($arr)
    {
//        var_dump('arr='.json_encode($arr));
        //检测客户端是否传递过来token数据
        if (!isset($arr['token']) || empty($arr['token'])) {
            $this->returnMsg(400, 'token不能为空');
        }

        if (strlen($arr['token']) > 100) {
            $this->returnMsg(400, 'token不能为空');
        }

        //这是客户端api传递过来的token
        $app_token = $arr['token'];
        //如果已经传递token数据，就删除token数据，生成服务端token与客户端的token做对比
        unset($arr['token']);

        $user_token = Db::name("admin_user")->where(['token' => $app_token])->find();
        if (empty($user_token)) {
            $this->returnMsg(401, '用户不存在');
        }

        $this->user['id'] = $user_token['uid'];
        $this->user['token'] = $app_token;
        $diff_time = $user_token['expire'];
        $this->body_data = $arr;

        if (time() > $diff_time) {
            $this->returnMsg(402, 'token已经过期');
        }
    }

    //返回信息
    protected function returnMsg($code, $msg = '', $data = [])
    {
        $return_data['status'] = $code;
        $return_data['msg'] = $msg;
        $return_data['result'] = $data;
        echo json_encode($return_data);die;
    }

    /**
     * [上传文件到服务器]
     * @param  [object] $file [文件资源]
     * @param  [string] $type [图片类型]
     * @return [string]       [图片在服务器的路径]
     */
    public function uploadFiles($file, $type = '')
    {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

        if ($info) {
            //获取图片路径
            $path = '/uploads/' . $info->getSaveName();
            $path = preg_replace('/\\\\/', '/', $path);
            //裁剪图片
            if (!empty($type)) {
                $this->imageEdit($path, $type);
            }
        } else {
            $this->returnMsg(400, $file->getError());
        }

        return $path;
    }

    /**
     * [图片裁剪]
     * @param  [string] $path [原图片的绝对路径]
     * @param  [string] $type [图片的类型]
     * @return [null]
     */
    public function imageEdit($path, $type)
    {
        $image = Image::open(ROOT_PATH . 'public' . $path);
        switch ($type) {
            case 'head_img':
                $image->thumb(200, 200, Image::THUMB_CENTER)->save(ROOT_PATH . 'public' . $path);
                break;
            case 'other_img':
                break;
        }
    }
}